package com.example.myapplication.apter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LeavecommentBean;
import com.example.myapplication.ShuJu.VoluntreelistBean;
import com.example.myapplication.movie.MoviedetailsActivity;
import com.example.myapplication.volunteerservice.RegistrationActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class VoluntreelistApter extends RecyclerView.Adapter<VoluntreelistApter.MyHolder> {
    private Context context;
    private List<VoluntreelistBean.RowsBean> list;

    private String TAG = "TAG";

    public VoluntreelistApter(Context context, List<VoluntreelistBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }


    @NonNull
    @Override
    public VoluntreelistApter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_voluntreelist, null);
        return new VoluntreelistApter.MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VoluntreelistApter.MyHolder holder, int position) {

        holder.volun_title.setText(list.get(position).getTitle());
        holder.volun_publisher.setText(list.get(position).getUndertaker());
        holder.volun_demand.setText(list.get(position).getRequireText());
        holder.volun_starttime.setText(list.get(position).getStartAt());

        //活动详情页
        holder.re_newsdislist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, RegistrationActivity.class);
                intent.putExtra(Commom.REGISTRATION_ID, String.valueOf(list.get(position).getId()));
                context.startActivity(intent);
            }
        });
        //报名
        holder.volun_signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject jsonObject = new JSONObject();
                Integer id = list.get(position).getId();
                boolean newState = true;
                try {
                    jsonObject.put("activityId",id);
                    jsonObject.put("newState",newState);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String JSON = jsonObject.toString();
                sendregistration(JSON);
            }
        });
    }

    //报名
    private void sendregistration(String  JSON) {
        Handler handler = new Handler();
        OKHttpTool.doNetRequest("prod-api/api/volunteer-service/register",
                OKHttpTool.POST,
                LeavecommentBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                LeavecommentBean leavecommentBean = (LeavecommentBean)obj;
                                if(leavecommentBean.getCode()==200){
                                    Toast.makeText(context,"报名成功",Toast.LENGTH_SHORT).show();
                                }else{
                                    Toast.makeText(context,"无法报名",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                },JSON
        );
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView volun_title;
        TextView volun_publisher;
        TextView volun_demand;
        TextView volun_starttime;
        RelativeLayout re_newsdislist;
        LinearLayout volun_signUp;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            volun_title = itemView.findViewById(R.id.volun_title);
            volun_publisher = itemView.findViewById(R.id.volun_publisher);
            volun_demand = itemView.findViewById(R.id.volun_demand);
            volun_starttime = itemView.findViewById(R.id.volun_starttime);
            re_newsdislist = itemView.findViewById(R.id.re_newsdislist);
            //报名
            volun_signUp = itemView.findViewById(R.id.volun_signUp);

        }
    }
}




